package com.shuorikeji.huohuo.ui.mh;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.Mh1Adapter;
import com.shuorikeji.huohuo.adapter.MhPlAdapter;
import com.shuorikeji.huohuo.adapter.MhTagAdapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.MhDetailInfoBean;
import com.shuorikeji.huohuo.bean.MhDetailInfoMainBean;
import com.shuorikeji.huohuo.bean.MhInfoBean;
import com.shuorikeji.huohuo.bean.MhInfoMainBean;
import com.shuorikeji.huohuo.bean.MhInfoSubBean;
import com.shuorikeji.huohuo.bean.MhPlInfoBean;
import com.shuorikeji.huohuo.bean.MhPlMainBean;
import com.shuorikeji.huohuo.bean.MhPlSubBean;
import com.shuorikeji.huohuo.bean.MhZjInfoBean;
import com.shuorikeji.huohuo.bean.MhZjInfoMainBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.common.Constant;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.utils.BusinessUtils;
import com.shuorikeji.huohuo.utils.DialogUtil;
import com.shuorikeji.huohuo.utils.JsonUtil;
import com.shuorikeji.huohuo.utils.SpacesItemDecoration;
import com.shuorikeji.huohuo.utils.ToastShowUtil;
import com.shuorikeji.huohuo.view.GridDividerItemDecoration;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class MhDetailActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_share)
    ImageView ivShare;
    @BindView(R.id.iv_jb)
    ImageView ivJb;
    @BindView(R.id.tv_tj_rs)
    TextView tvTjRs;//推荐人数
    @BindView(R.id.tv_mn_title)
    TextView tvMhTitle;
    @BindView(R.id.rv_mh_tag)
    RecyclerView rvMhTag;
    @BindView(R.id.tv_mh_pf)
    TextView tvMhPf;
    @BindView(R.id.rb_mh_pf)
    RatingBar rbMhPf;
    @BindView(R.id.tv_mh_hp_rs)
    TextView tvMhHpRs;//漫画好评人数
    @BindView(R.id.tv_mh_info)
    TextView tvMhInfo;
    @BindView(R.id.tv_mh_gx_state)
    TextView tvMhGxState;//漫画更新状态
    @BindView(R.id.tv_mh_ygx_zjs)
    TextView tvYgxs;//已更新章节数
    @BindView(R.id.tv_mh_new_gx_time)
    TextView tvMhNewGxTime;//最近更新时间
    @BindView(R.id.tv_mh_add_pl)
    TextView tvAddPl;//添加评论
    @BindView(R.id.rv_pl)
    RecyclerView rvPl;
    @BindView(R.id.tv_see_more_pl)
    TextView tvSeeMorePl;
    @BindView(R.id.rv_t_zz_mh)
    RecyclerView rvTzzMh;//同一作者更多漫画
    @BindView(R.id.ll_h_like)
    LinearLayout llHlike;
    @BindView(R.id.ll_ml)
    LinearLayout llMl;
    @BindView(R.id.rv_y_like_mh)
    RecyclerView rvYlike;
    @BindView(R.id.ll_download)
    LinearLayout llDownload;
    @BindView(R.id.ll_sc)
    LinearLayout llSc;
    @BindView(R.id.iv_sc)
    ImageView ivSc;
    @BindView(R.id.tv_sc)
    TextView tvSc;
    @BindView(R.id.tv_start_read)
    TextView tvStartRead;
    @BindView(R.id.iv_mh_fm)
    ImageView ivMhFm;

    private int MH_ID;
    private MhDetailInfoBean mhDetailInfo;
    private MhTagAdapter mhTagAdapter;
    private List<String> mhTagList = new ArrayList<>();
    private Mh1Adapter sameZzMhAdapter;
    private List<MhInfoBean> sameZzMhList = new ArrayList<>();
    private Mh1Adapter sameTypeMhAdapter;
    private List<MhInfoBean> sameTypeMhList = new ArrayList<>();
    private int curStypeMhPageNum;
    private MhInfoSubBean sameTypeMhBean;
    private int curMhZjListPage;
    private List<MhZjInfoBean> mhZjList = new ArrayList<>();
    private int mhAllZjCount;
    private Dialog mlDialog;
    private List<MhPlInfoBean> mhPlList = new ArrayList<>();
    private MhPlAdapter mhPlAdapter;
    private MhPlSubBean mhPlSubBean;//漫画评论

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_mh_detail);
    }

    @Override
    protected void initData() {
        initAdapter();
        initIntent();
        initClick();
    }

    private void initAdapter() {
        FlexboxLayoutManager manager = new FlexboxLayoutManager(this);
        manager.setFlexDirection(FlexDirection.ROW);
        manager.setFlexWrap(FlexWrap.WRAP);
        rvMhTag.addItemDecoration(new GridDividerItemDecoration(20));
        rvMhTag.setLayoutManager(manager);
        mhTagAdapter = new MhTagAdapter(MhDetailActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {

            }
        });
        rvMhTag.setAdapter(mhTagAdapter);

        rvTzzMh.setLayoutManager(new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false));
        rvTzzMh.addItemDecoration(new GridDividerItemDecoration(60));
        sameZzMhAdapter = new Mh1Adapter(MhDetailActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                MhInfoBean data = (MhInfoBean) object;
                MH_ID = data.getId();
                getMhDetail();
            }
        });
        rvTzzMh.setAdapter(sameZzMhAdapter);

        rvYlike.setLayoutManager(new GridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false));
        rvYlike.addItemDecoration(new GridDividerItemDecoration(60));
        sameTypeMhAdapter = new Mh1Adapter(MhDetailActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        MhInfoBean data = (MhInfoBean) object;
                        MH_ID = data.getId();
                        getMhDetail();
                        break;
                }
            }
        });
        rvYlike.setAdapter(sameTypeMhAdapter);

        rvPl.setLayoutManager(new LinearLayoutManager(MhDetailActivity.this, LinearLayoutManager.VERTICAL, false));
        rvPl.addItemDecoration(new SpacesItemDecoration(0, 0, 10, 0));
        mhPlAdapter = new MhPlAdapter(MhDetailActivity.this, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:

                        break;
                }
            }
        });
        rvPl.setAdapter(mhPlAdapter);
    }

    private void initIntent() {
        Intent intent = getIntent();
        MH_ID = intent.getIntExtra("MH_ID", -1);
        getMhDetail();
    }

    private void getMhDetail() {
        mPresenter.getMhDetail(true, MH_ID, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        MhDetailInfoMainBean data = (MhDetailInfoMainBean) object;
                        mhDetailInfo = data.getData();
                        curStypeMhPageNum = 1;
                        curMhZjListPage = 1;
                        showDetailInfo();
                        getSameZzMh();
                        getSameTypeMh();
                        getMhZjList(false);
                        getMhPlList();
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhDetailActivity.this, msg);
                }
            }
        });
    }

    /**
     * 获取漫画章节列表
     *
     * @param showMlDialog 是否显示目录dialog
     */
    private void getMhZjList(boolean showMlDialog) {
        mPresenter.getMhZjList(true, MH_ID, 20, curMhZjListPage, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        MhZjInfoMainBean data = (MhZjInfoMainBean) object;
                        tvYgxs.setText("已更新" + data.getData().getChapterList().getTotal() + "话");
                        mhAllZjCount = data.getData().getChapterList().getTotal();
                        if (curMhZjListPage == 1) {
                            mhZjList.clear();
                        }
                        mhZjList.addAll(data.getData().getChapterList().getData());
                        if (showMlDialog) {
                            DialogUtil.gxMhZjInfo(mhZjList);
                        }
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhDetailActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void getMhPlList() {
        mPresenter.getMhPlList(true, MH_ID, 1, 1, 20, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        MhPlMainBean data = (MhPlMainBean) object;
                        mhPlSubBean = data.getData();
                        mhPlList.clear();
                        mhPlList.addAll(data.getData().getData());
                        if (mhPlList != null && mhPlList.size() > 3) {
                            for (int i = mhPlList.size() - 1; i >= 0; i--) {
                                if (i >= 3) {
                                    mhPlList.remove(i);
                                }
                            }
                        }
                        mhPlAdapter.setData(mhPlList);
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhDetailActivity.this, msg);
                        break;
                }
            }
        });
    }

    /**
     * 同一作品类型
     */
    private void getSameTypeMh() {
        mPresenter.getAllMh(4, -1, 20, curStypeMhPageNum, "", "", mhDetailInfo.getCartoon_channel_id(), mhDetailInfo.getCartoon_category_id(),
                -1, -1, -1, -1, -1, -1, -1, -1, true, new ClickCallBack() {
                    @Override
                    public void onClick(int flag, Object object) {
                        switch (flag) {
                            case 1:
                                MhInfoMainBean data = (MhInfoMainBean) object;
                                sameTypeMhBean = data.getData();
                                if (curStypeMhPageNum == 1) {
                                    sameTypeMhList.clear();
                                }
                                sameTypeMhList.addAll(data.getData().getData());
                                sameTypeMhAdapter.setData(sameTypeMhList);
                                break;
                            case 2:
                                String msg = (String) object;
                                ToastShowUtil.showToastCenter(MhDetailActivity.this, msg);
                                break;
                        }
                    }
                });
    }

    /**
     * 同一作者漫画
     */
    private void getSameZzMh() {
        mPresenter.getAllMh(3, -1, 20, 1, "", mhDetailInfo.getAuthor(), -1, -1,
                -1, -1, -1, -1, -1, -1, -1, -1, true, new ClickCallBack() {
                    @Override
                    public void onClick(int flag, Object object) {
                        switch (flag) {
                            case 1:
                                MhInfoMainBean data = (MhInfoMainBean) object;
                                sameZzMhList.clear();
                                sameZzMhList.addAll(data.getData().getData());
                                sameZzMhAdapter.setData(sameZzMhList);
                                break;
                            case 2:
                                String msg = (String) object;
                                ToastShowUtil.showToastCenter(MhDetailActivity.this, msg);
                                break;
                        }
                    }
                });
    }

    private void showDetailInfo() {
        mhTagList.clear();
        mhTagList.addAll(mhDetailInfo.getTag_id());
        mhTagAdapter.setData(mhTagList);
        BusinessUtils.setLoadImg(MhDetailActivity.this, Constant.IMG_OSS_BASE_URL + mhDetailInfo.getCover_image(), ivMhFm);
        tvMhTitle.setText(mhDetailInfo.getName());
        tvMhInfo.setText(mhDetailInfo.getContent() + "\n\n作者：" + mhDetailInfo.getAuthor());
        tvMhPf.setText(mhDetailInfo.getScore());
        rbMhPf.setRating((Double.parseDouble(mhDetailInfo.getScore()) >= 5) ? 5 : (int) Double.parseDouble(mhDetailInfo.getScore()));
        tvMhGxState.setText((mhDetailInfo.getPublish_status() == 1) ? "连载中" : "已完结");
        tvMhNewGxTime.setText("最近更新：" + mhDetailInfo.getAddtime());
        ivSc.setImageResource((mhDetailInfo.getBookshelf() == 0) ? R.mipmap.icon_mh_wsc : R.mipmap.icon_mh_ysc);
        tvSc.setText((mhDetailInfo.getBookshelf() == 0) ? "收藏" : "已收藏");
    }

    private void showMlDialog() {
        mlDialog = DialogUtil.showMhMlDialog(MhDetailActivity.this, mhZjList, mhAllZjCount, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        //上一页
                        if (curMhZjListPage == 1) {
                            ToastShowUtil.showToastCenter(MhDetailActivity.this, "当前已经是第一页了");
                        } else {
                            curMhZjListPage = curMhZjListPage - 1;
                            getMhZjList(true);
                        }
                        break;
                    case 2:
                        //下一页
                        if (mhZjList.size() == mhAllZjCount) {
                            ToastShowUtil.showToastCenter(MhDetailActivity.this, "当前已经是最后一页了");
                        } else {
                            curMhZjListPage = curMhZjListPage + 1;
                            getMhZjList(true);
                        }
                        break;
                    case 3:
                        //开始看漫画
                        MhZjInfoBean data = (MhZjInfoBean) object;
                        Intent intent = new Intent(MhDetailActivity.this, SeeMhTempActivity.class);
                        intent.putExtra("MH_ID", MH_ID);
                        intent.putExtra("ZJ_ID", data.getId());
                        if (mhDetailInfo != null) {
                            intent.putExtra("IS_ADD_SJ", mhDetailInfo.getBookshelf());
                        }
                        if (mhPlSubBean != null) {
                            intent.putExtra("PLS", mhPlSubBean.getTotal());
                        }
                        startActivity(intent);
                        break;
                }
            }
        });
    }

    private void addSj() {
        mPresenter.addMhSj(true, MH_ID, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        mhDetailInfo.setBookshelf(1);
                        showDetailInfo();
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhDetailActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void deleteSj() {
        mPresenter.deleteMhSj(true, MH_ID, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        mhDetailInfo.setBookshelf(0);
                        showDetailInfo();
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhDetailActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void initClick() {
        ivBack.setOnClickListener(view -> {
            finish();
        });
        llHlike.setOnClickListener(view -> {
            if (sameTypeMhBean != null) {
                if (sameTypeMhList.size() < sameTypeMhBean.getTotal()) {
                    curStypeMhPageNum = curStypeMhPageNum + 1;
                    getSameTypeMh();
                } else {
                    ToastShowUtil.showToastCenter(MhDetailActivity.this, "暂无更多数据");
                }
            }
        });
        llMl.setOnClickListener(view -> {
            if (mhZjList != null && mhZjList.size() > 0) {
                showMlDialog();
            }
        });
        tvSeeMorePl.setOnClickListener(view -> {
            Intent intent = new Intent(MhDetailActivity.this, MhPlActivity.class);
            intent.putExtra("MH_DETAIL_INFO", JsonUtil.toJson(mhDetailInfo));
            startActivity(intent);
        });
        llSc.setOnClickListener(view -> {
            //收藏漫画
            if (mhDetailInfo != null) {
                if (mhDetailInfo.getBookshelf() == 0) {
                    //添加书架
                    addSj();
                } else {
                    //删除书架
                    deleteSj();
                }
            }
        });
        tvStartRead.setOnClickListener(view -> {
            if (mhZjList != null && mhZjList.size() > 0 && mhDetailInfo != null) {
                Intent intent = new Intent(MhDetailActivity.this, SeeMhTempActivity.class);
                intent.putExtra("MH_ID", MH_ID);
                intent.putExtra("ZJ_ID", mhZjList.get(0).getId());
                if (mhDetailInfo != null) {
                    intent.putExtra("IS_ADD_SJ", mhDetailInfo.getBookshelf());
                }
                if (mhPlSubBean != null) {
                    intent.putExtra("PLS", mhPlSubBean.getTotal());
                }
                startActivity(intent);
            } else {
                ToastShowUtil.showToastCenter(MhDetailActivity.this, "暂无数据，请稍后重试");
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        BusinessUtils.clearDialog(mlDialog);
    }
}